package com.yuebu.yuebubtlib.util;

import java.util.Locale;

/**
 * BLEByteUtil
 *
 * @author SJL
 * @date 2017/4/21
 */

public class BLEByteUtil {

    //以十六进制字符输出字节数组到日志,每两位字符进行分隔
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            if (i == src.length - 1) {
                stringBuilder.append(hv.toUpperCase(Locale.US));
            } else {
                stringBuilder.append(hv.toUpperCase(Locale.US)).append("");
            }
        }
        return stringBuilder.toString();
    }
    public static String bytesToMac(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            if (i == src.length - 1) {
                stringBuilder.append(hv.toUpperCase(Locale.US));
            } else {
                stringBuilder.append(hv.toUpperCase(Locale.US)).append(":");
            }
        }
        return stringBuilder.toString();
    }

    //截取一部分字节
    public static byte[] getSubbytes(byte[] bytes, int start, int len) {
        if (bytes.length < len || len == 0) {
            return null;
        } else if (bytes.length == len) {
            return bytes;
        }
        byte[] bs = new byte[len];
        for (int i = 0; i < len; i++) {
            bs[i] = bytes[start++];
        }
        return bs;
    }

    private final static byte[] hex = "0123456789ABCDEF".getBytes();

    /**
     * 字节转16进制
     *
     * @param b 字节数组
     * @return 16进制字符串
     */
    public static String Bytes2HexString(byte[] b) {
        byte[] buff = new byte[2 * b.length];
        for (int i = 0; i < b.length; i++) {
            buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
            buff[2 * i + 1] = hex[b[i] & 0x0f];
        }
        return new String(buff);
    }

    /**
     * SmartPen过滤
     *
     * @param scanRecord 蓝牙返回字节数组
     * @return 是否为SmartPen
     */
    public static boolean isToneSmartPen(byte[] scanRecord) {
        String ret = Bytes2HexString(scanRecord);
        int index = 0;
        if (ret.contains("FF") || ret.contains("ff")) {
            index = ret.indexOf("FF") / 2;
            byte[] b = new byte[4];
            b[0] = scanRecord[index + 1];
            b[1] = scanRecord[index + 2];
            b[2] = scanRecord[index + 3];
            b[3] = scanRecord[index + 4];
            String stmp = Bytes2HexString(b);
            if (stmp.equals("31323334")) {
                return true;
            }
        }

        return false;
    }

    /**
     * int转16进制字符串
     * @param i
     * @return
     */
    public static String int2HexString(int i) {
        return String.format("%6s", Integer.toHexString(i)).replace(' ', '0');
    }
}
